package com.theobserver.theobserver;

import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.GetCallback;

import java.util.ArrayList;

/**
 * Created by wssxk on 7/11/17.
 */

public class NewsSubscribFragment extends Fragment {
    Context mContext = null;
    AVUser cur_user = null;

    public void init(Context context)
    {
        mContext = context;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
    {
        View v = inflater.inflate(R.layout.news_column_layout, container, false);


        ListView lv = v.findViewById(R.id.listview_news);
        final NewsColumnAdapter adapter = new NewsColumnAdapter(mContext);
        lv.setAdapter(adapter);

        final SwipeRefreshLayout sl = (SwipeRefreshLayout) v.findViewById(R.id.swiperefreshlayout_news);
        sl.setRefreshing(true);
        //sl.setEnabled();

        final DrawerLayout drawer = ((FragmentActivity) mContext).findViewById(R.id.main_page_drawer);
        ImageView column_slider = v.findViewById(R.id.imageview_column_slider);
        column_slider.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawer.openDrawer(Gravity.LEFT);
            }
        });

        TextView subscrib_title = (TextView) v.findViewById(R.id.textview_column_title);
        subscrib_title.setText("我的订阅");

        View add_more = v.findViewById(R.id.relativelayout_news_addmore);
        add_more.setVisibility(View.GONE);


        cur_user = AVUser.getCurrentUser();

        if(cur_user != null)
        {
            Column.getSubscribColumn(cur_user, new ColumnCallBack() {
                @Override
                public void getColumn(ArrayList<Column> columns, int state, AVException e) {
                    sl.setRefreshing(false);
                    if(columns != null && columns.size() != 0)
                    {
                        //sl.setRefreshing(false);
                        adapter.column.clear();
                        adapter.column.addAll(columns);
                        adapter.notifyDataSetChanged();

                    }
                    else
                    {
                        adapter.column.clear();
                        adapter.notifyDataSetChanged();
                    }
                }
            });
        }
        else
        {
            Toast.makeText(mContext, "请先登陆", Toast.LENGTH_SHORT).show();
        }





        /*
        cur_user = new AVUser();
        cur_user.setObjectId("5961b956ac502e005819946c");
        cur_user.fetchInBackground(new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                Column.getSubscribColumn(cur_user, new ColumnCallBack() {
                    @Override
                    public void getColumn(ArrayList<Column> columns, int state, AVException e) {
                        sl.setRefreshing(false);
                        if(columns != null && columns.size() != 0)
                        {
                            //sl.setRefreshing(false);
                            adapter.column.clear();
                            adapter.column.addAll(columns);
                            adapter.notifyDataSetChanged();

                        }
                        else
                        {
                            adapter.column.clear();
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
            }
        });
        */
        ///-------------这里修改
        /*
        Column.getSubscribColumn(, new ColumnCallBack() {
            @Override
            public void getColumn(ArrayList<Column> columns, int state, AVException e) {
                sl.setRefreshing(false);
                if(columns != null)
                {
                    //sl.setRefreshing(false);
                    adapter.column.clear();
                    adapter.column.addAll(columns);
                    adapter.notifyDataSetChanged();

                }
            }
        });
        */

        sl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Column.getSubscribColumn(cur_user, new ColumnCallBack() {
                    @Override
                    public void getColumn(ArrayList<Column> columns, int state, AVException e) {
                        sl.setRefreshing(false);
                        if(columns != null)
                        {
                            //sl.setRefreshing(false);
                            adapter.column.clear();
                            adapter.column.addAll(columns);
                            adapter.notifyDataSetChanged();

                        }
                        else
                        {
                            adapter.column.clear();
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent t_intent = new Intent(mContext, ColumnDetailActivity.class);
                t_intent.putExtra("objectId",
                        ((NewsItemFrameLayout) view.findViewById(R.id.framelayout_column_item)).column.cur_column.getObjectId());
                startActivity(t_intent);
            }
        });


        return v;
    }
}
